package com.ruoyi.DegreePoints.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.DegreePoints.mapper.DegreePointsMapper;
import com.ruoyi.assignments.mapper.ExpertAssignmentsMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.DegreePoints.domain.DegreePoints;
import com.ruoyi.DegreePoints.service.IDegreePointsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学位点Controller
 * 
 * @author ruoyi
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/DegreePoints/points")
public class DegreePointsController extends BaseController
{
    @Autowired
    private IDegreePointsService degreePointsService;

    @Autowired
    private DegreePointsMapper degreePointsMapper;

    @Autowired
    private ExpertAssignmentsMapper expertAssignmentsMapper;

    /**
     * 查询学位点列表
     */
//    @PreAuthorize("@ss.hasPermi('DegreePoints:points:list')")
    @GetMapping("/list")
    public TableDataInfo list(DegreePoints degreePoints)
    {
        startPage();
        List<DegreePoints> list = degreePointsService.selectDegreePointsList(degreePoints);
        return getDataTable(list);
    }

    /**
     * 导出学位点列表
     */
    @PreAuthorize("@ss.hasPermi('DegreePoints:points:export')")
    @Log(title = "学位点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DegreePoints degreePoints)
    {
        List<DegreePoints> list = degreePointsService.selectDegreePointsList(degreePoints);
        ExcelUtil<DegreePoints> util = new ExcelUtil<DegreePoints>(DegreePoints.class);
        util.exportExcel(response, list, "学位点数据");
    }

    /**
     * 获取学位点详细信息
     */
//    @PreAuthorize("@ss.hasPermi('DegreePoints:points:query')")
    @GetMapping(value = "/{pointId}")
    public AjaxResult getInfo(@PathVariable("pointId") Long pointId)
    {
        return success(degreePointsService.selectDegreePointsByPointId(pointId));
    }

    //根据userID 查询学位点信息
    @GetMapping(value = "/byUId")
//    @PreAuthorize("@ss.hasPermi('DegreePoints:points:query')")
    public AjaxResult getInfoByUId()
    {
        return success(degreePointsMapper.selectByUId(getLoginUser().getUserId()));
    }

    //查询专家评审学位点
    @GetMapping(value = "/byExpertId")
//    @PreAuthorize("@ss.hasPermi('DegreePoints:points:query')")
    public AjaxResult getInfoByExpertId()
    {
        return success(degreePointsMapper.selectByExpertId(getLoginUser().getUserId()));
    }
    /**
     * 新增学位点
     */
//    @PreAuthorize("@ss.hasPermi('DegreePoints:points:add')")
    @Log(title = "学位点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DegreePoints degreePoints) throws Exception {
        try {
            degreePoints.setPointId(getLoginUser().getUserId());
            degreePoints.setUserId(getLoginUser().getUserId());
            degreePoints.setStatus(1L);
            return toAjax(degreePointsService.insertDegreePoints(degreePoints));
        }catch (Exception e){
            return error("编号重复,请更正");
        }


    }

    /**
     * 修改学位点
     */
//    @PreAuthorize("@ss.hasPermi('DegreePoints:points:edit')")
    @Log(title = "学位点", businessType = BusinessType.UPDATE)
    @Transactional
    @PutMapping
    public AjaxResult edit(@RequestBody DegreePoints degreePoints)
    {
        //修改专家分配中间表
        if(degreePoints.getExpertId() != null && degreePoints.getExpertId() != 0)
            expertAssignmentsMapper.updateByPointId(degreePoints.getPointId(),degreePoints.getExpertId());
        return toAjax(degreePointsService.updateDegreePoints(degreePoints));
    }

    /**
     * 删除学位点
     */
    @PreAuthorize("@ss.hasPermi('DegreePoints:points:remove')")
    @Log(title = "学位点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pointIds}")
    public AjaxResult remove(@PathVariable Long[] pointIds)
    {
        return toAjax(degreePointsService.deleteDegreePointsByPointIds(pointIds));
    }
}
